package com.yupi.mianshiya.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.mianshiya.common.BaseResponse;
import com.yupi.mianshiya.common.ErrorCode;
import com.yupi.mianshiya.common.ResultUtils;
import com.yupi.mianshiya.exception.ThrowUtils;
import com.yupi.mianshiya.model.dto.feedback.feedbackQueryRequest;
import com.yupi.mianshiya.model.entity.Feedback;
import com.yupi.mianshiya.model.entity.User;
import com.yupi.mianshiya.model.enums.FeedbackStatus;
import com.yupi.mianshiya.service.FeedbackService;
import com.yupi.mianshiya.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "留言反馈接口")
@RestController
@RequestMapping("/feedback")
@Slf4j
public class FeedbackController {
    @Resource
    private FeedbackService feedbackService;

    @Resource
    private UserService userService;

    @ApiOperation(value = "添加留言反馈")
    @PostMapping("/add")
    public BaseResponse<Boolean> addFeedback(@RequestBody Feedback feedback, HttpServletRequest request) {
        User user = userService.getLoginUser(request);

        ThrowUtils.throwIf(feedback.getContent() == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(feedback.getContactInfo() == null, ErrorCode.PARAMS_ERROR);
        boolean save = feedbackService.save(feedback);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);

    }

    @ApiOperation(value = "管理员修改留言反馈")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateFeedback(@RequestBody Feedback feedback, HttpServletRequest request) {
        boolean admin = userService.isAdmin(request);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);
        ThrowUtils.throwIf(feedback.getId() == null, ErrorCode.PARAMS_ERROR);
        String statusValue = feedback.getStatus();
        FeedbackStatus statusEnum = FeedbackStatus.getEnumByValue(statusValue);
        ThrowUtils.throwIf(statusEnum == null, ErrorCode.PARAMS_ERROR, "无效的状态值: " + statusValue);

        boolean update = feedbackService.updateById(feedback);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);

    }

    @ApiOperation(value = "管理员获删除言反馈列表")
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteFeedback(@RequestParam("id") Long id, HttpServletRequest request) {
        boolean admin = userService.isAdmin(request);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);
        boolean remove = feedbackService.removeById(id);
        ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @ApiOperation("管理员获取留言反馈列表")
    @GetMapping("/list")
    public BaseResponse<Page<Feedback>> getFeedbackList(feedbackQueryRequest feedbackQueryRequest, HttpServletRequest request) {
        boolean admin = userService.isAdmin(request);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);
        int current = feedbackQueryRequest.getCurrent();
        int pageSize = feedbackQueryRequest.getPageSize();
        QueryWrapper<Feedback> queryWrapper = feedbackService.getQueryWrapper(feedbackQueryRequest);
        Page<Feedback> page = feedbackService.page(new Page<>(current, pageSize), queryWrapper);
        return ResultUtils.success(page);
    }

    @ApiOperation("管理员根据id获取留言反馈")
    @GetMapping("/get")
    public BaseResponse<Feedback> getFeedbackById(@RequestParam("id") Long id, HttpServletRequest request) {
        boolean admin = userService.isAdmin(request);
        ThrowUtils.throwIf(!admin, ErrorCode.NO_AUTH_ERROR);
        Feedback feedback = feedbackService.getById(id);
        ThrowUtils.throwIf(feedback == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(feedback);
    }

}
